#include "kbc.h" 
  
 int kbc_init(int debug); 
 void mouse_disable(void); 
  
 volatile unsigned char scancode = 0; 
  
 int  write_kbc_cmd( unsigned data){ 
         return write_kbc(CMD_REG, data); 
 } 
  
 int has_DOS_mouse_ISR(); 
  
 int  write_kbc_data(unsigned data){ 
         int tentativas = 0; 
         while(tentativas < 3){ 
                 if(!write_kbc(DATA_REG, data)) 
                         return ACK; 
                 else if(read_kbc() == Resend) 
                         tentativas++; 
                 else 
                         return -1; 
         } 
         return Error; 
 } 
  
 int  write_aux(unsigned cmd){ 
         int tentativas = 0; 
         while(tentativas < 3){ 
                 if(!write_kbc_cmd(cmd)) 
                         return ACK; 
                 else if(read_kbc() == Resend) 
                         tentativas++; 
                 else 
                         return -1; 
         } 
         return Error; 
 } 
  
 int  read_kbc(void){ 
         int stat, tempo = 0; 
         while(tempo < KBC_TIMEOUT){ 
                 stat = inportb(STAT_REG); 
                 if((stat & OBF) != 0){ 
                         if((stat & (PAR_ERR | TO_ERR)) == 0) 
                                 return inportb(DATA_REG); 
                         else 
                                 return -1; 
                 } 
                 tempo++; 
                 delay(1); 
         } 
         return -1; 
 } 
  
 int  write_kbc(unsigned adr, unsigned data){ 
         int stat, tempo = 0; 
         while(tempo < KBC_TIMEOUT){ 
                 stat = inportb(STAT_REG); 
                 if((stat & IBF) == 0){ 
                         outportb(adr, data); 
                         return 0; 
                 } 
                 tempo++; 
                 delay(1); 
         } 
         return -1; 
 } 

 //********************************Funcoes novas********************* 
 unsigned char getKey() { 
          
         unsigned char tmp = 0; 
          
         if (scancode & (1<<7)) 
                 tmp = scancode; 
          
         scancode = 0; 
          
         return tmp; 
 } 
  
 void clearBuffer() { 
         scancode = 0; 
 } 
  
 char *keyPressed(unsigned char key){ 
         //scancodes do teclado usados 
         //Left  203 
         //Right         205 
         //Up       200 
         //Down  208 
         switch (key){ 
                 case 0x4b:  
                         return "left"; 
                  
                 case 0x4d: 
                         return "right"; 
                  
                 case 0x48: 
                         return "up"; 
                          
                 case 0x50: 
                         return "down"; 
                          
                 case 0x1: 
                         return "escape"; 
                          
                 case 0x56: 
                         return "enter"; 
                          
                 case 0x02: 
                         return "um"; //numeros sobre o teclado 
  
                 case 0x03: 
                         return "dois"; 
                          
                 case 0x04: 
                         return "tres"; 
                          
                 default: 
                         return NULL; 
         } 
 }
